1 package net.sourceforge.simplegamenet.dice; 2 3 import javax.swing.*; 4 import net.sourceforge.simplegamenet.specs.gui.GamePanel; 5 import net.sourceforge.simplegamenet.util.proportionlayout.ProportionConstraints; 6 import net.sourceforge.simplegamenet.util.proportionlayout.ProportionLayout; 7 8 public class DicePanel extends GamePanel implements DiceSettingOptions { 9 10 private DicePlayerClient dicePlayerClient; 11 private DiceScorePlayPanel diceScorePlayPanel; 12 private DiceScoreTotalPanel diceScoreTotalPanel; 13 private DicePlayPanel dicePlayPanel; 14 private JLabel playerTurnLabel = new JLabel("", (int) CENTER_ALIGNMENT); 15 16 public DicePanel(DicePlayerClient dicePlayerClient) { 17 this.dicePlayerClient = dicePlayerClient; 18 diceScorePlayPanel = new DiceScorePlayPanel(dicePlayerClient); 19 diceScoreTotalPanel = new DiceScoreTotalPanel(dicePlayerClient); 20 dicePlayPanel = new DicePlayPanel(dicePlayerClient); 21 ProportionLayout layout = new ProportionLayout(); 22 layout.appendColumn(5); 23 layout.appendColumn(0, 1.0); 24 layout.appendColumn(5); 25 layout.appendRow(5); 26 layout.appendRow(0, 9.0); 27 layout.appendRow(5, 1.0); 28 layout.appendRow(0, ProportionLayout.NO_PROPORTION); 29 layout.appendRow(5, 1.0); 30 layout.appendRow(0, ProportionLayout.NO_PROPORTION); 31 layout.appendRow(5); 32 setLayout(layout); 33 add(diceScorePlayPanel, new ProportionConstraints(1, 1)); 34 add(playerTurnLabel, new ProportionConstraints(1, 2)); 35 add(diceScoreTotalPanel, new ProportionConstraints(1, 3)); 36 add(dicePlayPanel, new ProportionConstraints(1, 5)); 37 } 38 39 public void setupDicePanels() { 40 diceScoreTotalPanel.setPlayerNicknames(); 41 diceScorePlayPanel.setScorePlayCases(); 42 } 43 44 public void highlightDice(int diceNumber) { 45 dicePlayPanel.getDice(diceNumber).highlightDice(); 46 } 47 48 public void enableScorePlayPanel() { 49 diceScorePlayPanel.setMyTurn(true); 50 } 51 52 public void enablePanelForPlay(Integer dicePlayerID) { 53 dicePlayPanel.setFirstTimesRolled(); 54 if (dicePlayerID.equals(dicePlayerClient.getPlayerID()) 55 && !diceScorePlayPanel.gameOver()) { 56 playerTurnLabel.setText("It's your turn!"); 57 dicePlayPanel.setEnabled(true); 58 dicePlayPanel.resetDice(); 59 diceScorePlayPanel.setMyTurn(true); 60 for (int i = 0; i < 5; i++) { 61 dicePlayPanel.getDice(i).setMyTurn(true); 62 } 63 } else if (!dicePlayerID.equals(dicePlayerClient.getPlayerID()) 64 && !diceScorePlayPanel.gameOver()) { 65 playerTurnLabel.setText(dicePlayerClient.getDicePlayerNickname(dicePlayerID) + 66 " is playing!"); 67 dicePlayPanel.setEnabled(false); 68 dicePlayPanel.setDiceUnclicked(); 69 diceScorePlayPanel.setMyTurn(false); 70 for (int i = 0; i < 5; i++) { 71 dicePlayPanel.getDice(i).setMyTurn(false); 72 } 73 } else { 74 dicePlayerClient.sendDicePacket(new DicePacket(DicePacketCodes.GAME_OVER, null)); 75 } 76 } 77 78 public void disablePanel() { 79 dicePlayPanel.setEnabled(false); 80 dicePlayPanel.setDiceUnclicked(); 81 diceScorePlayPanel.setMyTurn(false); 82 for (int i = 0; i < 5; i++) { 83 dicePlayPanel.getDice(i).setMyTurn(false); 84 } 85 } 86 87 public void setRolledDice(int[] rolledDice) { 88 dicePlayPanel.setTimesRolled(); 89 for (int i = 0; i < rolledDice.length; i++) { 90 if (rolledDice[i] != 6) { 91 dicePlayPanel.getDice(i).setDiceValue(rolledDice[i]); 92 } 93 } 94 } 95 96 public void setTotalScores(int[] playerTotals) { 97 diceScorePlayPanel.fillTotals(playerTotals[0], playerTotals[1]); 98 diceScoreTotalPanel.fillTotal(playerTotals[2]); 99 } 100 101 public void setFilledScore(int[] filledScore) { 102 diceScorePlayPanel.fillScore(filledScore); 103 } 104 105 public int askQuestion() { 106 int answer = JOptionPane.showConfirmDialog(this, 107 "Do you want to put a zero in that case?", "0 points input", 108 JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); 109 return answer; 110 } 111 112 public void adjustSettingValues(int scoreToCount, int[] settingValues) { 113 diceScorePlayPanel.adjustSettingValue(scoreToCount == 6 ? 0 : scoreToCount - 9, 114 settingValues[scoreToCount == 6 ? 2 : scoreToCount - 7]); 115 } 116 117 public DiceScorePlayPanel getDiceScorePlayPanel() { 118 return diceScorePlayPanel; 119 } 120 121 public void showWinner(int[] winner) { 122 if (winner[1] == 0 || !dicePlayerClient.containsPlayerID( 123 dicePlayerClient.getDiceParticipantIDs()[winner[0]])) { 124 playerTurnLabel.setText("It's a tie game"); 125 } else { 126 playerTurnLabel.setText("The winner is " + 127 dicePlayerClient.getDicePlayerNickname( 128 dicePlayerClient.getDiceParticipantIDs()[winner[0]]) + 129 " with " + winner[1] + " points!"); 130 } 131 } 132 133 }